package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.impl.DishServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.security.Key;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result saveWithDishFlovors(@RequestBody DishDTO dishDTO) {
        //1.接收参数
        log.info("新增菜品参数：{}", dishDTO);
        //2.调用service层方法，处理业务逻辑
        dishService.saveWithDishFlovors(dishDTO);
        //删除缓存
        String key = "dish_" + dishDTO.getCategoryId();
        caceRedis(key);
        //3.响应数据
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        //1.接收参数
        log.info("分页查询参数:{}", dishPageQueryDTO);
        //2.调用service层方法，处理业务逻辑
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        //3.响应数据
        return Result.success(pageResult);
    }

    /**
     * 菜品的批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品的批量删除")
    // public Result delete(String ids){
    public Result delete(@RequestParam List<Long> ids) {
        //1.接收参数
        log.info("批量删除参数：{}", ids);
        //2.调用service层方法，处理业务逻辑
        dishService.deleteBatch(ids);
        //删除缓存
        caceRedis("dish_*");
        //3.响应数据
        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        //1.接收参数
        log.info("获取id参数：{}", id);
        //2.调用service层方法，处理业务逻辑
        DishVO dishVO = dishService.getById(id);
        //3.响应数据
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        //1.接收参数
        log.info("获取修改参数：{}", dishDTO);
        //2.调用service层方法，处理业务逻辑
        dishService.updateById(dishDTO);
        //删除缓存
        caceRedis("dish_*");
        //3.响应数据
        return Result.success();
    }

    /**
     * 菜品起售停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        // 1.接收参数
        log.info("接收参数：{}，{}", status, id);
        // 2.调用service层，实现业务逻辑
        dishService.startOrStop(status, id);
        //删除缓存
        caceRedis("dish_*");
        // 3.返回数据给前端
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {
        // 1.接收参数
        log.info("接收到参数:{}", categoryId);
        // 2.调用service层，实现业务逻辑
        List<Dish> list = dishService.list(categoryId);
        // 3.返回数据给前端
        return Result.success(list);
    }

    /**
     * 清除缓存 删除redis数据
     *
     * @param key
     */
    private void caceRedis(String key) {
        Set keys = redisTemplate.keys(key);
        redisTemplate.delete(keys);
    }
}
